package com.linzelin.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.linzelin.po.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 君未洋
 */
@RestController
public class UserController {

    @RequestMapping(value = "/json")
    //这个就是配置json的中文输出了
    //@ResponseBody
    //使用这个就不会通过视图解析器去跳转,
    //这个方法会直接返回字符串
    public String json() throws JsonProcessingException {
        //创建一个jackson对象
        ObjectMapper mapper = new ObjectMapper();
        User user = new User("林先生",18,"男");

        String string = mapper.writeValueAsString(user);
        return string;
        //{"name":"???","age":18,"sex":"?"}
        //返回是这么一个结果
        //中文乱码先无视下 , 可以发现现在是以json的形式将user对象转化为一个字符串了
    }

    @RequestMapping("/json2")
    public String json2() throws JsonProcessingException {
       // ObjectMapper mapper = new ObjectMapper();

        //测试下集合转化为JSON
        List<User> list = new ArrayList<>();
        User user1 = new User("林先生1",18,"男");
        User user2 = new User("林先生2",12,"男");
        User user3 = new User("林先生3",13,"男");
        User user4 = new User("林先生4", 14, "男");

        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);


        //String str = mapper.writeValueAsString(list);
        //return str;
        /*java集合转JSON字符串*/
        String string = JSON.toJSONString(list);
        System.out.println(string);
        System.out.println("--------------");
        /*[{"age":18,"name":"林先生1","sex":"男"},
        {"age":12,"name":"林先生2","sex":"男"},
        {"age":13,"name":"林先生3","sex":"男"},
        {"age":14,"name":"林先生4","sex":"男"}]*/

        String str2 = JSON.toJSONString(user2);
        /*JSON字符串转java对象*/
        User user = JSON.parseObject(str2, User.class);
        System.out.println(user);
        System.out.println("--------------");
        //User(name=林先生2, age=12, sex=男)

        /*Java对象转JSON对象*/
        JSONObject json = (JSONObject) JSON.toJSON(user);
        System.out.println(json);
        System.out.println("---------------");
        //{"sex":"男","name":"林先生2","age":12}

        /*JSON对象转为JAVA对象*/
        User user5 = JSON.toJavaObject(json, User.class);
        System.out.println(user5);
        //User(name=林先生2, age=12, sex=男)
        return "I AM FINE";
    }


    @RequestMapping("/json3")
    public String json3() throws JsonProcessingException {
        //这里测试下Jackson自带的时间转化
        ObjectMapper mapper = new ObjectMapper();


       /*
       //使用java的方式来配置转化格式
       Date date = new Date();
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        //配置格式转化
       return mapper.writeValueAsString(sdf.format(date));*/

        //不使用时间戳的方式
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        //自定义日期格式对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //指定日期格式
        mapper.setDateFormat(sdf);

        Date date = new Date();
        String str = mapper.writeValueAsString(date);

        return str;
    }
}

